package com.lzx.makeFriend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzx.common.utils.R;
import com.lzx.makeFriend.entity.Invite;
import com.lzx.makeFriend.entity.vo.Problem;
import com.lzx.makeFriend.entity.vo.User;
import com.lzx.makeFriend.feign.AnswerClient;
import com.lzx.makeFriend.feign.UserClient;
import com.lzx.makeFriend.mapper.InviteMapper;
import com.lzx.makeFriend.service.InviteService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lzx
 * @since 2023-01-03
 */
@Service
public class InviteServiceImpl extends ServiceImpl<InviteMapper, Invite> implements InviteService {
    @Resource
    private InviteMapper inviteMapper;

    @Resource
    private UserClient userClient;

    @Resource
    private AnswerClient answerClient;

    //邀请用户回答问题
    @Override
    public R inviteUser(Invite invite) {
        QueryWrapper<Invite> wrapper = new QueryWrapper<>();
        wrapper.eq("recipient", invite.getRecipient()).eq("inviter", invite.getInviter()).eq("problem_id", invite.getProblemId());
        Invite vo = inviteMapper.selectOne(wrapper);
        if (vo != null) {
            vo.setCreateTime(new Date());
            inviteMapper.updateById(vo);
            return R.ok();
        } else {
            invite.setCreateTime(new Date());
            inviteMapper.insert(invite);
            return R.ok().message("邀请成功");
        }
    }

    //获取推荐邀请列表
    @Override
    public R getRecommendList(int userId) {
        List<User> userList = userClient.getUserByReplyCount(userId);
        return R.ok().data("list", userList);
    }

    //获取当前用户收到的好友邀请
    @Override
    public R getInviteList(int userId, int current, int size) {
        QueryWrapper<Invite> wrapper = new QueryWrapper<>();
        wrapper.eq("recipient", userId);
        List<Invite> invites = inviteMapper.selectList(wrapper);
        Page<Invite> page = new Page<>();
        //总数
        int totalInvite = invites.size();
        if (size > totalInvite) {
            size = totalInvite;
        }
        int maxPage = 0;
        if (size > 0) {
            //求出最大页数，防止越界
            maxPage = totalInvite % size == 0 ? totalInvite / size : totalInvite / size + 1;
        }
        if (current > maxPage) {
            current = maxPage;
        }
        //封装数据
        List<Invite> inviteList = invites.stream().peek(invite -> {
            User user = userClient.getUserinfoByIdToOtherServe(Long.valueOf(invite.getInviter()));
            Problem problem = answerClient.getProblem(invite.getProblemId());
            invite.setUser(user);
            invite.setMessage(problem.getTitle());
        }).collect(Collectors.toList());
        //当前页第一条数据的下标
        int currentIndex = current > 1 ? (current - 1) * size : 0;
        ArrayList<Invite> arrayList = new ArrayList<>();
        for (int i = 0; i < size && currentIndex + i < totalInvite; i++) {
            arrayList.add(inviteList.get(currentIndex + i));
        }
        page.setCurrent(current).setSize(size).setTotal(totalInvite).setRecords(arrayList);
        //统计通知邀请中的数量
        wrapper.eq("is_agree", 0);
        Long count = inviteMapper.selectCount(wrapper);
        Map<String, Object> map = new HashMap<>();
        List<Invite> list = page.getRecords();//当前页数据
        long total = page.getTotal();//总记录数
        map.put("list", list);
        map.put("total", total);
        map.put("count", count);
        return R.ok().data("map", map);
    }

    //用户同意或拒绝邀请
    @Override
    public R isAgreeInvite(Long id, int isAgree) {
        Invite invite = inviteMapper.selectById(id);
        invite.setIsAgree(isAgree);
        inviteMapper.updateById(invite);
        return R.ok().message("保存成功");
    }
}
